package views;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import objects.Constituent;
import objects.Feature;
import objects.Lexicon;

public class ViewFeatures extends JFrame {
	
	/* Data variables */
	private Constituent currentConstituent;
	private Lexicon currentLexicon;
	
	/** Component variables **/
	
	private JPanel contentPane;
	
	private JTable table;
	private DefaultTableModel tableModel;
	
	private JComboBox<String> cmbLabelChooser;
	private JComboBox<String> cmbFeatureTypeChooser;
	
	private JButton btnGo;
	private JButton btnAddFeature;
	private JButton btnAddValue;
	private JButton btnSave;
	
	public ViewFeatures() {
		currentConstituent = null;
		currentLexicon = null;
		
		setTitle("Features");
		setMinimumSize(new Dimension(480, 200));
		setSize(new Dimension(600, 300));
		setLocationRelativeTo(null);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panOptions = new JPanel();
		contentPane.add(panOptions, BorderLayout.NORTH);
		panOptions.setLayout(new BorderLayout(0, 0));
		
		JPanel panOptionsLeft = new JPanel();
		panOptions.add(panOptionsLeft, BorderLayout.WEST);
		panOptionsLeft.setLayout(new BorderLayout(5, 5));
		
		JPanel panOptionsSave = new JPanel();
		panOptionsLeft.add(panOptionsSave, BorderLayout.SOUTH);
		
		btnSave = new JButton("Save");
		panOptionsSave.add(btnSave);
		btnSave.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		JPanel panOptionsCenter = new JPanel();
		panOptions.add(panOptionsCenter, BorderLayout.CENTER);
		panOptionsCenter.setLayout(new BorderLayout(0, 0));
		
		JPanel panOptionsNav = new JPanel();
		panOptionsCenter.add(panOptionsNav, BorderLayout.SOUTH);
		
		cmbFeatureTypeChooser = new JComboBox<>();
		cmbFeatureTypeChooser.addItem("Semantic Rep");
		cmbFeatureTypeChooser.addItem("Lexicon");
		panOptionsNav.add(cmbFeatureTypeChooser);
		cmbFeatureTypeChooser.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		cmbLabelChooser = new JComboBox<>();
		panOptionsNav.add(cmbLabelChooser);
		cmbLabelChooser.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		btnGo = new JButton("Go");
		panOptionsNav.add(btnGo);
		btnGo.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		JPanel panOptionsRight = new JPanel();
		panOptions.add(panOptionsRight, BorderLayout.EAST);
		panOptionsRight.setLayout(new BorderLayout(0, 0));
		
		JPanel panOptionsAddLbl = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panOptionsAddLbl.getLayout();
		flowLayout.setVgap(0);
		panOptionsRight.add(panOptionsAddLbl, BorderLayout.NORTH);
		
		JLabel lblAdd = new JLabel("Add");
		panOptionsAddLbl.add(lblAdd);
		
		JPanel panOptionsAdd = new JPanel();
		panOptionsRight.add(panOptionsAdd, BorderLayout.CENTER);
		
		btnAddFeature = new JButton("Feature");
		panOptionsAdd.add(btnAddFeature);
		btnAddFeature.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		btnAddValue = new JButton("Value");
		panOptionsAdd.add(btnAddValue);
		btnAddValue.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		JPanel panContent = new JPanel();
		contentPane.add(panContent, BorderLayout.CENTER);
		
		tableModel = new DefaultTableModel(new String[][] {}, new String[] {"Editable", "Name", "Value"});
		
		table = new JTable(tableModel) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		TableColumnAdjuster adjuster = new TableColumnAdjuster(table);
		adjuster.setDynamicAdjustment(true);
		adjuster.adjustColumns();
		panContent.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(table);
		panContent.add(scrollPane);
	}
	
	public void loadConstituent(Constituent constituent) {
		currentConstituent = constituent;
		
		clearTable();
		addRows(currentConstituent.getFeatures().getFeatureList());
	}
	
	public void loadLexicon(Lexicon lexicon) {
		currentLexicon = lexicon;
		
		clearTable();
		addRows(currentLexicon.getFeatures().getFeatureList());
	}
	
	/** Table methods **/
	
	public Feature getSelectedFeature() {
		Feature feature = null;
		String name = null;
		
		try {
			name = (String) table.getValueAt(table.getSelectedRow(), tableModel.findColumn("Name"));
			
			for (Feature currentFeature : currentConstituent.getFeatures().getFeatureList()) {
				if (currentFeature.getName().equals(name)) {
					feature = currentFeature;
					break;
				}
			}
		}
		catch (Exception e) {}
		
		return feature;
	}
	
	public Feature getSelectedFeatureLexicon() {
		Feature feature = null;
		String name = null;
		
		try {
			name = (String) table.getValueAt(table.getSelectedRow(), tableModel.findColumn("Name"));
			
			for (Feature currentFeature : currentLexicon.getFeatures().getFeatureList()) {
				if (currentFeature.getName().equals(name)) {
					feature = currentFeature;
					break;
				}
			}
		}
		catch (Exception e) {}
		
		return feature;
	}
	
	public void addRows(Feature feature) {
		String name = feature.getName();
		String editable = (feature.isEditable() ? "Yes" : "No");
		for (String value : feature.getValueList()) {
			tableModel.addRow(new String[] {editable, name, value});
		}
	}
	
	public void addRows(ArrayList<Feature> features) {
		for (Feature feature : features) {
			addRows(feature);
		}
	}
	
	public void clearTable() {
		while (tableModel.getRowCount() > 0) {
			tableModel.removeRow(0);
		}
	}
	
	/** ComboBox methods **/
	
	public void setConstituentsForChooser(ArrayList<Constituent> constituents) {
		ArrayList<String> labels = new ArrayList<>();
		
		for (Constituent constituent : constituents) {
			labels.add(constituent.getLabel());
		}
		
		setLabelsForChooser(labels);
	}
	
	public void setLexiconsForChooser(ArrayList<Lexicon> lexicons) {
		ArrayList<String> labels = new ArrayList<>();
		
		for (Lexicon lexicon : lexicons) {
			labels.add(lexicon.getLabel());
		}
		
		setLabelsForChooser(labels);
	}
	
	public void setLabelsForChooser(ArrayList<String> labels) {
		for (String label : labels) {
			cmbLabelChooser.addItem(label);
		}
	}
	
	public String getSelectedFeatureType() {
		return cmbFeatureTypeChooser.getSelectedItem().toString();
	}
	
	public int getSelectedLabelIndex() {
		return cmbLabelChooser.getSelectedIndex();
	}
	
	public void clearCombo() {
		cmbLabelChooser.removeAllItems();
	}
	
	/** Get class data **/
	
	public Constituent getCurrentConstituent() {
		return currentConstituent;
	}
	
	public Lexicon getCurrentLexicon() {
		return currentLexicon;
	}
	
	/** Add listeners **/
	
	public void addSaveListener(ActionListener actionListener) {
		btnSave.addActionListener(actionListener);
	}
	
	public void addNavListener(ActionListener actionListener) {
		btnGo.addActionListener(actionListener);
	}
	
	public void addAddFeatureListener(ActionListener actionListener) {
		btnAddFeature.addActionListener(actionListener);
	}
	
	public void addAddValueListener(ActionListener actionListener) {
		btnAddValue.addActionListener(actionListener);
	}
	
	public void addTableMouseListener(MouseListener mouseListener) {
		table.addMouseListener(mouseListener);
	}
	
	public void addComboBoxItemListener(ItemListener itemListener) {
		cmbFeatureTypeChooser.addItemListener(itemListener);
	}
	
}
